<?php

namespace app\common\model;

use think\Model;
use think\Validate;


/**
 * 学生班级关系表模型
 */
class StudentClassesRelation extends Model
{
    // 表名,不含前缀
    protected $name = 'student_classes_relation';

    /**
     * 信息
     */
    public function dataInfo($dis)
    {
        $res = $this->where($dis)->find();
        return $res;
    }

    /**
     * 列表
     */
    public function dataList($dis ,$field = '*')
    {
        $res = $this->where([]);

        foreach ($dis as $field => $condition) {
            if(Validate::is($condition, 'array') && isset($condition[0]) && $condition[0] === 'in') {
                // 处理 IN 条件
                $res->whereIn($field, $condition[1]);
            } else {
                // 普通条件
                $res->where($field, $condition);
            }
        }
        
        return $res->select();
    }

    /**
     * 添加
     */
    public function dataAdd($data)
    {
        $res = $this->insert($data);
        return $res;
    }

    /**
     * 数量
     */
    public function dataCount($dis, $groupBy = null)
    {
        $query = $this;
        
        if ($dis instanceof \Closure) {
            $query = $dis($query);
        } elseif (is_string($dis)) {
            $query = $query->whereRaw($dis);
        } elseif (is_array($dis)) {
            $query = $query->where($dis);
        }
        
        // 如果有分组条件
        if ($groupBy) {
            return $query->group($groupBy)
                    ->column('count(*) as count', $groupBy);
        }
        
        return $query->count();
    }

    /**
     * 成员ID
     */
    public function dataMemberId($dis, $foreignKey)
    {
        $res = $this->where($dis)->column($foreignKey);
        return $res;
    }
}
